export default {
	namespaced : true,
	state : {
		// 头部搜索历史记录
		headerSearch: [],
		// 频道搜索历史记录
		channelSearch: [],
		// 热搜数据
		hotSearch: {
			id: undefined,
			text: '',
			url: ''
		}
	},
	mutations:{
		// 如果历史记录里没有，就添加一条历史记录，有则将其置顶
		pushSearch(state, payload){
			let {name, val} = payload,
					target = state[name]
			let has = target.findIndex((item,index)=>item.text==val.text);
			if(has != -1) target.splice(has, 1);
			target.unshift(val);
			sessionStorage.setItem('search/'+name, JSON.stringify(target));
		},
		// 删除一条搜索历史
		removeSearch(state, payload){
			let {name, index} = payload,
					target = state[name]
			target.splice(index, 1);
			sessionStorage.setItem('search/'+name, JSON.stringify(target));
		},
		// 清除历史搜索
		clearSearch(state, payload){
			state[payload] = [];
			sessionStorage.setItem('search/'+payload, JSON.stringify([]));
		},
		// 初始化历史记录时获取搜索记录数据
		getSearchHistory(state, payload){
			let res = sessionStorage.getItem('search/'+payload) || '[]';
			state[payload] = JSON.parse(res);
		},
		// 获取热搜数据
		getHotSearch(state, payload){
			state.hotSearch.id = payload.id;
			state.hotSearch.text = payload.show_name;
			state.hotSearch.url = payload.url;
		},
	}
}